package app.action;

import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

import javax.swing.JCheckBox;

import app.textSrc.TextSource;
import app.ui.QuestionUI;

public class TickAnswerAction extends MouseAdapter {
	private QuestionUI mPanel;
	private JCheckBox[][] jCheckBoxs;

	public TickAnswerAction(QuestionUI mPanel) {
		super();
		this.mPanel = mPanel;
		this.jCheckBoxs = mPanel.getjCheckBoxes();
	}

	@Override
	public void mouseReleased(MouseEvent e) {
		if (e.getButton() != 1 || mPanel.isShowAnsewr()) {
			return;
		}
		JCheckBox jCheckBox = (JCheckBox) e.getSource();
		setThisCheckBoxProperties(jCheckBox);

		// get and set the others related checkbox properties
		int indexI = 0, indexJ = 0;
		for (int i = 0; i < 3; i++) {
			for (int j = 0; j < 4; j++) {
				if (jCheckBox == jCheckBoxs[i][j]) {
					indexI = i;
					indexJ = j;
					break;
				}
			}
		}

		for (int j = 0; j < 4; j++) {
			if (j != indexJ) {
				setOtherCheckBoxProperties(jCheckBoxs[indexI][j]);
			}
		}

		// count the question and answer
		int index = mPanel.getIndex() * 3;
		index += indexI;
		char ans = TextSource.ansLetStr.charAt(indexJ);

		mPanel.getTextSource().getCusAnsStr()[index] = String.valueOf(ans);
	}

	private void setThisCheckBoxProperties(JCheckBox jCheckBox) {
		if (!jCheckBox.isSelected()) {
			jCheckBox.setSelected(true);
		} else {
			jCheckBox.setFont(mPanel.getmFrame().getAppInit().getFont3());
			jCheckBox.setBackground(mPanel.getmFrame().getAppInit().getCol2());
		}
	}

	private void setOtherCheckBoxProperties(JCheckBox jCheckBox) {
		if (jCheckBox.isSelected()) {
			jCheckBox.setSelected(false);
			jCheckBox.setBackground(mPanel.getmFrame().getAppInit().getCol1());
			jCheckBox.setFont(mPanel.getmFrame().getAppInit().getFont2());
		}
	}

}
